﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BaseballGame.Models;

namespace BaseballGame.Controllers
{
	public class RunClass
	{
		public RunClass()
		{
			game_entitiesDbContext db = new game_entitiesDbContext();
			Random random = new Random();
			var allLeague = from m in db.leagues select m;
			List<league> lstLeague = new List<league>();
			foreach (league a in allLeague)
			{
				lstLeague.Add(a);
			}

			if (lstLeague.Count > 0)
			{
				foreach (league a in lstLeague)
					runSingleLeague(random, a, db);
			}

		}

		private void runSingleLeague(Random random, league a, game_entitiesDbContext db)
		{
			int team1ID = a.team1ID;
			int team2ID = a.team2ID;
			int team3ID = a.team3ID;
			int team4ID = a.team4ID;
			GameEngine game;
			var allGames = from c in db.gameStats select c;

			GameStats lastHomeGame = null;
			GameStats lastAwayGame = null;

			foreach (GameStats SingleGameStat in allGames)
			{
				if (SingleGameStat.HomeTeamID == team1ID)
					lastHomeGame = SingleGameStat;
				else if (SingleGameStat.AwayTeamID == team1ID)
					lastAwayGame = SingleGameStat;
			}

			if (lastHomeGame == null && lastAwayGame == null)
			{
				//no games played, Team 1 home vs Team 2
				game = new GameEngine(team1ID, team2ID, random);
				game = new GameEngine(team3ID, team4ID, random);
			}
			else if (lastHomeGame == null)
			{
				//played away but never home
				if (team2ID == lastAwayGame.HomeTeamID)
				{
					game = new GameEngine(team1ID, team3ID, random);
					game = new GameEngine(team2ID, team4ID, random);
				}
				else if (team3ID == lastAwayGame.HomeTeamID)
				{
					game = new GameEngine(team1ID, team4ID, random);
					game = new GameEngine(team2ID, team3ID, random);
				}
				else if (team4ID == lastAwayGame.HomeTeamID)
				{
					game = new GameEngine(team1ID, team2ID, random);
					game = new GameEngine(team3ID, team4ID, random);
				}
			}
			else if (lastAwayGame == null)
			{
				//played home but never away..
				//played away but never home
				if (team2ID == lastHomeGame.AwayTeamID)
				{
					game = new GameEngine(team3ID, team1ID, random);
					game = new GameEngine(team4ID, team2ID, random);
				}
				else if (team3ID == lastHomeGame.AwayTeamID)
				{
					game = new GameEngine(team4ID, team1ID, random);
					game = new GameEngine(team3ID, team2ID, random);
				}
				else if (team4ID == lastHomeGame.AwayTeamID)
				{
					game = new GameEngine(team2ID, team1ID, random);
					game = new GameEngine(team4ID, team3ID, random);
				}
			}
			else
			{
				//normal case
				if (lastHomeGame.id > lastAwayGame.id)
				{
					//last game was home
					if (team2ID == lastHomeGame.AwayTeamID)
					{
						game = new GameEngine(team3ID, team1ID, random);
						game = new GameEngine(team4ID, team2ID, random);
					}
					else if (team3ID == lastHomeGame.AwayTeamID)
					{
						game = new GameEngine(team4ID, team1ID, random);
						game = new GameEngine(team3ID, team2ID, random);
					}
					else if (team4ID == lastHomeGame.AwayTeamID)
					{
						game = new GameEngine(team2ID, team1ID, random);
						game = new GameEngine(team4ID, team3ID, random);
					}
				}
				else
				{
					//last game was away
					if (team2ID == lastAwayGame.HomeTeamID)
					{
						game = new GameEngine(team1ID, team3ID, random);
						game = new GameEngine(team2ID, team4ID, random);
					}
					else if (team3ID == lastAwayGame.HomeTeamID)
					{
						game = new GameEngine(team1ID, team4ID, random);
						game = new GameEngine(team2ID, team3ID, random);
					}
					else if (team4ID == lastAwayGame.HomeTeamID)
					{
						game = new GameEngine(team1ID, team2ID, random);
						game = new GameEngine(team3ID, team4ID, random);
					}
				}
			}

		}
	}
}